/**
 * org.vz.product.collect.maintain.service.impl
 * @since : jdk 1.8
 */
package org.vz.product.collect.maintain.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.vz.product.collect.maintain.dao.GetServiceByIdDao;
import org.vz.product.collect.maintain.domain.ServiceExceptionsEntity;
import org.vz.product.collect.maintain.domain.entity.GetServiceResult;
import org.vz.product.collect.maintain.domain.entity.GetServiceResult.Services;
import org.vz.product.collect.maintain.service.GetServiceByIdAndDayService;

/**
 * @author Morik is Yongxu.Wang 根据id查询服务详情
 * @CreateTime 2017年10月17日 11:25:26
 * @version 1.0.0
 * @description
 */
@Service
public class GetServiceByIdAndDayServiceImpl implements
		GetServiceByIdAndDayService {
	@Autowired
	private GetServiceByIdDao getServiceByIdDao;
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private SimpleDateFormat hoverSDF = new SimpleDateFormat("HH");// 小时
	private SimpleDateFormat mininSDF = new SimpleDateFormat("mm");// 分钟
	private SimpleDateFormat daySDF = new SimpleDateFormat("dd");// 天
	private SimpleDateFormat monthSDF = new SimpleDateFormat("MM");// 月

	@Override
	public Services getService(String starTime, String endTime,
			String serviceId, int type) {
		Services se = new GetServiceResult().new Services();
		se.servicesName = serviceId;
		long start = 0, end = 0;
		try {
			start = sdf.parse(starTime).getTime();
			end = sdf.parse(endTime).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long during = end - start;
		long time = 0;
		time = during / (6 + 6);

		for (int i = 0; i < 6; i++) {
			String e, et;
			String s = sdf.format(start);
			String st = getTime(type, start);
			if (i == 5) {
				e = sdf.format(end);
				et = getTime(type, end);
			} else {
				e = sdf.format(start += time);
				et = getTime(type, start += time);
			}
			se.dataTime.add(st + "-" + et);
			Map<String, String> pr = new HashMap<String, String>();
			pr.put("starTime", s);
			pr.put("endTime", e);
			pr.put("interfaceId", serviceId);
			List<ServiceExceptionsEntity> datas = getServiceByIdDao
					.getSQLManager().select("messagelog.getExceputionsById",
							ServiceExceptionsEntity.class, pr);
			if (datas.size() > 0) {
				se.excepNum.add((double) datas.get(0).getNumber());
			} else {
				se.excepNum.add(0d);
			}
			List<ServiceExceptionsEntity> exception = getServiceByIdDao
					.getSQLManager().select("messagelog.getSerceCountById",
							ServiceExceptionsEntity.class, pr);
			if (exception.size() > 0) {
				se.useNum.add((double) exception.get(0).getNumber());
			} else {
				se.useNum.add(0d);
			}
		}
		return se;
	}

	private String getTime(int type, long time) {
		String timeStr = null;
		switch (type) {
		case 0: // 24小时
			timeStr = hoverSDF.format(time);
			break;
		case 1: // 60分钟
			timeStr = mininSDF.format(time);
			break;
		case 2: // 一个月
			timeStr = daySDF.format(time);
			break;
		case 3: // 一年
			timeStr = monthSDF.format(time);
			break;
		case 4: // 全部
			timeStr = hoverSDF.format(time);
			break;
		default:
			break;
		}
		return timeStr;
	}
}
